﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FFSupplier.Common.Configuration;
using FFSupplier.Common.CustomContext;
using FFSupplier.ServiceCore.Impl.CrmConnection;
using FFSupplier.ServiceCore.Interface.CrmConnection;
using FFSupplier.ServiceCore.Model;

namespace FFSupplier.ServiceCore.Factory.Basic
{
    public class ProxyFactory : IFactory<ICrmOrganizationServiceProxy>
    {
        public ICrmOrganizationServiceProxy Create()
        {
            ContextGeneric<ICrmOrganizationServiceProxy> contextOrganizationXrm = new ContextGeneric<ICrmOrganizationServiceProxy>();
            ICrmOrganizationServiceProxy orgService = contextOrganizationXrm.Current;
            if (orgService == null)
            {
                ContextGeneric<CrmOrgaizationConfiguration> contextConfigurationXrm = new ContextGeneric<CrmOrgaizationConfiguration>();
                CrmOrgaizationConfiguration config = contextConfigurationXrm.Current;
                if (config == null)
                {
                    IConfiguration appSetting = new AppSettingConfiguration();
                    config = new CrmOrgaizationConfiguration();
                    config.ServerUserName = appSetting.GetConfigurationData("crmusername");
                    config.ServerUserPassword = appSetting.GetConfigurationData("crmuserpassword");
                    config.ServerUserDomin = appSetting.GetConfigurationData("crmuserdomin");
                    config.ServerUrl = appSetting.GetConfigurationData("crmserverurl");
                    config.CrmDataBase = appSetting.GetConfigurationData("connCrm");
                    //string strAdministratorId = appSetting.GetConfigurationData("crmadministratorid");
                    //Guid administratorId;
                    //if (Guid.TryParse(strAdministratorId, out administratorId))
                    //{
                    //    config.CrmAdministratorId = Guid.Parse(strAdministratorId);
                    //}
                    //int tokenSeconds;
                    //string strTokenSeconds = appSetting.GetConfigurationData("tokenseconds");
                    //if (int.TryParse(strTokenSeconds, out tokenSeconds))
                    //{
                    //    config.TokenSeconds = int.Parse(strTokenSeconds);
                    //}
                    config.CRMUrl = appSetting.GetConfigurationData("crmurl");
                    //int begin;
                    //string strBegin = appSetting.GetConfigurationData("secyrityconfigurationbegin");
                    //if (int.TryParse(strBegin, out begin))
                    //{
                    //    config.SecurityConfigurationBegin = int.Parse(strBegin);
                    //}
                    //int end;
                    //string strEnd = appSetting.GetConfigurationData("secyrityconfigurationend");
                    //if (int.TryParse(strEnd, out end))
                    //{
                    //    config.SecurityConfigurationEnd = int.Parse(strEnd);
                    //}

                    //string strDay = appSetting.GetConfigurationData("manualshareexpireday");
                    //int day;
                    //if (int.TryParse(strDay, out day))
                    //{
                    //    config.ManualShareExpireDay = int.Parse(strDay);
                    //}
                    contextConfigurationXrm.Init(() => config);
                }
                orgService = new CrmOrganizationServiceProxy(config);
                contextOrganizationXrm.Init(() => orgService);
            }
            return orgService;
        }
    }
}
